package cn.gson.crm.controller.custom;

import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.gson.crm.controller.BaseServlet;
import cn.gson.crm.model.dao.CustomDao;
import cn.gson.crm.model.entity.Custom;
import cn.gson.crm.model.entity.Degree;
import cn.gson.crm.vo.CustomVo;

/**
 * 查看客户详情
 */
@WebServlet("/custom/details")
public class CustomDetailsServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	private CustomDao customDao = new CustomDao();
	String id = "";
	List<Map<String, Object>> degreeList = null;
	String jsp = "custom/custom_details.jsp";

	protected void get(HttpServletRequest request, HttpServletResponse response) throws Exception {
		degreeList = customDao.getDegreeAll();
		for (Map<String, Object> map : degreeList) {
			System.out.println(map+"  这是map");
		}
		id = request.getParameter("id");
		CustomVo custom = customDao.getOneCustomByid(id);
		request.setAttribute("degreeList", degreeList);
		request.setAttribute("custom", custom);
		forward("custom/custom_details.jsp");
	}

	protected void post(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String Name = request.getParameter("Name");
		String Gender = request.getParameter("Gender");
		String Tel = request.getParameter("Tel");
		String QQ = request.getParameter("QQ");
		String ParentName = request.getParameter("ParentName");
		String ParentTel = request.getParameter("ParentTel");
		String Source = request.getParameter("Source");
		String Degree = request.getParameter("Degree");
		String Address = request.getParameter("Address");
		Custom customUpdate = new Custom();
		customUpdate.setCustomName(Name);
		customUpdate.setCustomGender(Gender);
		customUpdate.setCustomAddress(Address);
		customUpdate.setCustomTel(Tel);
		if (Degree != "" || !Degree.isEmpty()) {
			customUpdate.setCustomDegreeId(Integer.parseInt(Degree));
		}
		customUpdate.setCustomQQ(QQ);
		customUpdate.setCustomParentName(ParentName);
		customUpdate.setCustomParentTel(ParentTel);
		String resource = Source.substring(0, 2);
		System.out.println(Name);
		customUpdate.setCustomSource(resource);

		if (Name == null || Name.isEmpty()) {
			request.setAttribute("custom", customUpdate);
			errorForward("姓名不能为空", jsp);
			return;
		} else {
			Pattern pattern = Pattern.compile("[\u4E00-\u9FA5]{2,4}");
			Matcher mqq = pattern.matcher(Name);
			boolean bqq = mqq.matches();
			if (!bqq) {
				request.setAttribute("custom", customUpdate);
				errorForward("学生姓名格式不对", jsp);
				return;
			}
		}
		if (Gender == null || Gender.isEmpty()) {
			request.setAttribute("custom", customUpdate);
			errorForward("性别不能为空", jsp);
			return;

		}
		if (Tel == null || Tel.isEmpty()) {
			request.setAttribute("custom", customUpdate);
			errorForward("电话不能为空", jsp);
			return;
		} else {
			Pattern patternTell = Pattern.compile("^1[0-9]\\d{9}$");
			Matcher mTell = patternTell.matcher(Tel);
			boolean bTell = mTell.matches();
			if (!bTell) {
				request.setAttribute("custom", customUpdate);
				errorForward("电话格式不对", jsp);
				return;
			}
		}
		if (QQ == null || QQ.isEmpty()) {
			request.setAttribute("custom", customUpdate);
			errorForward("qq不能为空", jsp);
			return;
		} else {
			Pattern pqq = Pattern.compile("[1-9][0-9]{4,14}");
			Matcher mqq = pqq.matcher(QQ);
			boolean bqq = mqq.matches();
			if (!bqq) {
				request.setAttribute("custom", customUpdate);
				errorForward("QQ格式不对", jsp);
				return;
			}
		}
		if (ParentName == null || ParentName.isEmpty()) {
			request.setAttribute("custom", customUpdate);
			errorForward("家长姓名不能为空", jsp);
			return;
		} else {
			Pattern pattern = Pattern.compile("[\u4E00-\u9FA5]{2,4}");
			Matcher mqq = pattern.matcher(ParentName);
			boolean bqq = mqq.matches();
			if (!bqq) {
				request.setAttribute("custom", customUpdate);
				errorForward("学生家长姓名格式不对", jsp);
				return;
			}
		}
		if (ParentTel == null || ParentTel.isEmpty()) {
			request.setAttribute("custom", customUpdate);
			errorForward("家长电话不能为空", jsp);
			return;
		} else {
			Pattern patternTell = Pattern.compile("^1[0-9]\\d{9}$");
			Matcher mTell = patternTell.matcher(ParentTel);
			boolean bTell = mTell.matches();
			if (!bTell) {
				request.setAttribute("custom", customUpdate);
				errorForward("家长电话格式不对", jsp);
				return;
			}
		}
		if ((Tel != null && !Tel.isEmpty()) && (ParentTel != null && !ParentTel.isEmpty())) {
			if (Tel.equals(ParentTel)) {
				request.setAttribute("custom", customUpdate);
				errorForward("客户电话和客户家长电话不能一样", jsp);
				return;
			}
		}
		if (Source == null || Source.isEmpty()) {
			request.setAttribute("custom", customUpdate);
			errorForward("来源不能为空", jsp);
			return;
		}
		if (Degree == null || Degree.isEmpty()) {
			request.setAttribute("custom", customUpdate);
			errorForward("学历不能为空", jsp);
			return;
		}
		if (Address == null || Address.isEmpty()) {
			request.setAttribute("custom", customUpdate);
			errorForward("地址不能为空", jsp);
			return;
		}
		// System.out.println(id);
		boolean result = customDao.updateCustomById(customUpdate, id);

		// System.out.println(result);
		if (result) {
			redirect("custom");
		} else {
			request.setAttribute("custom", customUpdate);
			errorForward("保存失败", jsp);
		}
	}

}
